package com.central.apps.handler.project;

import com.central.app.common.base.BaseHandler;
import com.central.app.common.util.DateUtils;
import com.central.app.service.HandlerService;
import com.central.apps.service.TaskChangeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 处理任务变更明细管理业务逻辑
 *
 * @author lidachao
 * @date 2023/4/26
 */
@Component
@Slf4j
public class TaskChangeDetailHandler extends BaseHandler {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private TaskChangeService taskChangeService;

    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if ("delTaskDeli".equals(type)) {
            // 删除任务交付物
            Integer id = MapUtils.getInteger(params, "id");
            handlerService.updateBySql("delete from gq_taskdelichg where id = " + id);
        }
        return null;
    }

    @Override
    public String validateSave(String app, String objName, Map<String, Object> data) throws Exception {
        if ("gq_taskchgdtl".equals(objName)) {
            String startTime = MapUtils.getString(data, "startTime");
            String endTime = MapUtils.getString(data, "endTime");

            boolean bool = DateUtils.isWeekEnd(startTime);
            if (bool) {
                return "开始日期不能为周六日!";
            }
            bool = DateUtils.isWeekEnd(endTime);
            if (bool) {
                return "结束日期不能为周六日!";
            }
        }
        return null;
    }

    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if ("gq_taskchgdtl".equals(objName)) {
            Integer id = MapUtils.getInteger(data, "id");
            Integer proId = MapUtils.getInteger(data, "proid");
            Integer tcId = MapUtils.getInteger(data, "tcid");
            Integer parentId = MapUtils.getInteger(data, "parentId");
            String isTempTask = MapUtils.getString(data, "istemptask");
            Integer level = 1;
            Integer chgLevel = 1;
            Integer parentLevel = 0;
            Integer parentChgLevel = 0;

            if (parentId > 0) {
                Map<String, Object> parentTaskMap = handlerService.findOneBySql("select level, taskchglevel, istemptask from gq_taskchgdtl where tcid =" + tcId + " and id = " + parentId);

                if (parentTaskMap != null) {
                    parentLevel = MapUtils.getInteger(parentTaskMap, "level");
                    parentChgLevel = MapUtils.getInteger(parentTaskMap, "taskchglevel");
                    level = parentLevel + 1;
                    chgLevel = parentChgLevel + 1;

                    StringBuilder sql = new StringBuilder();
                    sql.append("update gq_taskchgdtl set level = ").append(level).append(", parentLevel = ").append(parentLevel).append(", ")
                            .append("taskchglevel = ").append(chgLevel).append(", taskchgparentlevel = ").append(parentChgLevel);

                    String pIsTempTask = MapUtils.getString(parentTaskMap, "istemptask");
                    if ("0".equals(isTempTask) && "1".equals(pIsTempTask)) {
                        sql.append(", istemptask = '").append(pIsTempTask).append("'");
                    }
                    sql.append(" where id = ").append(id);

                    // 更新子级层级
                    handlerService.updateBySql(sql.toString());
                }
            }

            // 重新计算工期
            String startTime = MapUtils.getString(data, "startTime");
            String endTime = MapUtils.getString(data, "endTime");
            long duration = DateUtils.daysBetweenExpWeekEnd(startTime, endTime);
            handlerService.updateBySql("update gq_taskchgdtl set duration = " + duration + " where id = " + id);

            if ("1".equals(isTempTask)) {
                handlerService.updateBySql("update gq_taskchgdtl set istemptask = '" + isTempTask + "' where tcid = " + tcId + " and parentId = " + id);
            }

            // 重新整理记录id
            taskChangeService.updateRecordId(proId, tcId);
            // 重新整理父子关系
            taskChangeService.updParentId(proId, tcId);
            // 更新前置任务信息
            taskChangeService.updPredecessors(proId, tcId);
        }
    }
}
